<?php
include('header.php');
if ($session->logged_in) 
{
	if ($session->isAdmin()) 
	{?>
        <div id="form_container">
        <form id="form" class="appnitro" onclick="return executeOnSubmit();">
        <div class="form_description">
        <h2>Organization</h2>
        <p>View and Edit the Selected Organization</p>
        </div>
        <p><a href="new_org_register.php">Add a new Organization</a></p>  
        <?php
        /* 
        Displays all data from 'organizations_info' table
        This is a modified version of view.php that includes pagination
        */
                
        // number of results to show per page
        $per_page = 27;
                
        // figure out the total pages in the database
        $result = mysql_query("SELECT * FROM organizations ORDER BY orgName ASC");
        $total_results = mysql_num_rows($result);
        $total_pages = ceil($total_results / $per_page);
        
        // check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
        if (isset($_GET['page']) && is_numeric($_GET['page']))
        {
            $show_page = $_GET['page'];
                        
            // make sure the $show_page value is valid
            if ($show_page > 0 && $show_page <= $total_pages)
            {
                $start = ($show_page -1) * $per_page;
                $end = $start + $per_page; 
            }
            else
            {
                // error - show first set of results
                $start = 0;
                $end = $per_page; 
            }               
        }
        else
        {
            // if page isn't set, show first set of results
            $start = 0;
            $end = $per_page; 
        }
                
        // display pagination
                
        echo "<p><b>View Page:</b> ";
        for ($i = 1; $i <= $total_pages; $i++)
        {
            echo "<a href='org_list.php?page=$i'>$i</a> ";
        }
        echo "</p>";
                      
        // display data in table
        echo "<div class='form_body' style='height:82%'>
			<table border='1' cellpadding='2' cellspacing='0'>
			<tr> <th align='left' width='640px'>Organization Name</th> <th></th> <th></th></tr>";
        
        // loop through results of database query, displaying them in the table 
        for ($i = $start; $i < $end; $i++)
        {
            // make sure that PHP doesn't try to show results that don't exist
            if ($i == $total_results) { break; }
                
            // echo out the contents of each row into a table
            echo "<tr>";
            echo '<td>' . mysql_result($result, $i, 'orgName') . '</td>';
            echo '<td><a href="edit_org_info.php?id=' . mysql_result($result, $i, 'orgID') . '">Edit</a></td>';
            echo '<td><a href="delete_org_info.php?id=' . mysql_result($result, $i, 'orgID') . '" >Delete</a></td>';
            echo "</tr>"; 
        }
        // close table>
        echo "</table></div>"; 
                
        // pagination        
        ?>
        </form>
        </div>
        <?php 
	}
	else 
	{?>
		<div id="form_container">
		<form id="form" class="appnitro">
		<div class="form_description">
		<h2>Access Denied!</h2>
		<p>You don't have the right permission to access this page.</p>
		</div>
		</form>
		</div>	
<?php	}
}
else 
{?>
	<div id="form_container">
    <form id="form" class="appnitro">
    <div class="form_description">
    <h2>Access Denied!</h2>
    <p>You must login to access this page.</p>
    </div>
    </form>
    </div>
<?php
}
include('footer.php'); ?>